package com.dk.service.order;

import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.integral.TbIntegralMapper;
import com.dk.mapper.pay.TbPayLogMapper;
import com.dk.pojo.integral.TbIntegral;
import com.dk.pojo.pay.TbPayLog;
import com.dk.service.integral.IntegralData;
import com.dk.service.integral.IntegralService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Component
public class IntegralServiceImpl implements IntegralService {
    @Resource
    private TbPayLogMapper payLogMapper;
    @Resource
    private TbIntegralMapper integralMapper;

    @Override
    public int inserIntegral(IntegralData integralData, Long userId) {
        // 根据id查询是否存在该用户的积分
        TbIntegral integral = integralMapper.selectByPrimaryKey(userId);
        if (integral == null) {
            // 如果没有插入
            TbIntegral tbIntegral = new TbIntegral();
            tbIntegral.setUserId(userId);
            long price = Long.parseLong(integralData.getPrice());
            Long integralNum = price * 100;
            tbIntegral.setIntegralNumber(integralNum);
            tbIntegral.setCreateTime(new Date());
            tbIntegral.setUpdateTime(new Date());
            return integralMapper.insertSelective(tbIntegral);
        } else {
            // 如果有更新
            long price = Long.parseLong(integralData.getPrice());
            Long integralNum = price * 100;
            integral.setUpdateTime(new Date());
            integral.setIntegralNumber(integralNum);
            return integralMapper.updateByPrimaryKeySelective(integral);
        }

    }
}
